<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_right">
          <div>
            <el-button icon="Back" @click="router.push({ name: 'module_business_browse_list_category_list' })">
              {{ $t('common.return') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_table_main">
        <el-table :data="dataList" v-loading="dataListLoading" stripe>
          <el-table-column prop="id" :label="i18n('common.id')" width="70">
          </el-table-column>

          <el-table-column :label="$t('browse.name_type')" width="100">
            <template v-slot="scope">
              {{ scope.row.role_text }}
            </template>
          </el-table-column>

          <el-table-column :label="i18n('browse.browse_name')" min-width="200">
            <template v-slot="scope">
              <dl class="table_dl" v-if="1 == scope.row.role_id">
                <dt>
                  <el-avatar :size="45" :src="scope.row.user.avatar" />
                </dt>
                <dd class="table_dl_dd_all_30">
                  {{ scope.row.user.nickname }}
                </dd>
                <dd class="table_dl_dd_all_16_gray">
                  <span v-if="scope.row.user.organization">
                    {{ scope.row.user.organization.title }}
                  </span>
                </dd>
              </dl>
              <dl class="table_dl" v-if="2 == scope.row.role_id">
                <dt>
                  <el-avatar :size="45" :src="scope.row.contact.avatar" />
                </dt>
                <dd class="table_dl_dd_all_30">
                  {{ scope.row.contact.name }}
                </dd>
                <dd class="table_dl_dd_all_16_gray">
                  <span v-if="scope.row.contact.client">
                    {{ scope.row.contact.client.name }}
                  </span>
                </dd>
              </dl>
            </template>
          </el-table-column>

          <el-table-column :label="i18n('browse.module')" min-width="200">
            <template v-slot="scope">
              <dl class="table_dl">
                <dt>
                  <el-image class="cx-list-image" :src="scope.row.picture" loading="lazy" lazy>
                    <template #error>
                      <div class="image-slot">
                        <el-icon><Picture /></el-icon>
                      </div>
                    </template>
                  </el-image>
                </dt>
                <dd class="table_dl_dd_all_30">
                  【{{ scope.row.module }}】 {{ scope.row.title }}
                </dd>
                <dd class="table_dl_dd_all_16_gray">
                  <el-link @click="dialogVisible = true;loadQrcodeUrl(scope.row.path, scope.row.organization_id)" :underline="false">
                    <span>点击查看</span>
                    <el-icon class="mr5"><ArrowRight /></el-icon>
                  </el-link>
                </dd>
              </dl>
            </template>
          </el-table-column>

          <el-table-column prop="duration_text" :label="i18n('browse.duration')" width="100">
          </el-table-column>

          <el-table-column prop="total" :label="i18n('browse.total')" width="100">
          </el-table-column>

          <el-table-column :label="i18n('browse.create_time')" width="160">
            <template v-slot="scope">
              <dl class="table_dl">
                <dd class="table_dl_dd_all_30">
                  {{ scope.row.create_time_text }}
                </dd>
                <dd class="table_dl_dd_all_16_gray">
                  {{ scope.row.create_time }}
                </dd>
              </dl>
            </template>
          </el-table-column>
        </el-table>
        <div class="admin_table_main_pagination">
          <el-pagination
            @size-change="sizeChangeHandle"
            @current-change="currentChangeHandle"
            :page-size="pageSize"
            :total="totalPage"
            :current-page="pageIndex"
            background
            layout="prev, pager, next,jumper,total">
          </el-pagination>
        </div>
      </div>
    </div>

    <el-dialog v-model="dialogVisible" title="在小程序上查看" width="300" center>
      <div class="text-center mt20">
        <el-image class="qrcode" :src="qrcode_url"/>
      </div>
    </el-dialog>
  </div>
</template>

<script setup name="browse-list">
  import { useBase } from "@/hooks/base/useBase"
  import { useTable } from "@/hooks/table/useTable"

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('business/browse')

  const dialogVisible = ref(false)
  const qrcode_url = ref('')

  const dataForm = reactive({
    category_id: '',
    type: '',
    module: '',
    create_time: '',
  })

  const {
    dataList,
    pageIndex,
    pageSize,
    totalPage,
    dataListLoading,
    sizeChangeHandle,
    currentChangeHandle,
    getDataList,
    handleStatus,
    deleteHandle,
  } = useTable(model.value, dataForm)

  const loadQrcodeUrl = (path, organization_id) => {
    $http({
      url: $http.adornUrl('/business/qrcode/mini/data'),
      method: 'get',
      params: $http.adornParams({
        organization_id: organization_id,
        path: path,
      }),
    }).then(({ data }) => {
      if (data && data.status === 200) {
        qrcode_url.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  onMounted(() => {
    if(query.category_id) {
      dataForm.category_id = parseInt(query.category_id)
    }

    getDataList(model)
  })
</script>

<style scoped lang="scss">
  .qrcode {
    width: 10vw;
    height: 10vw;
  }
</style>
